package com.example.remoteassistance;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.projection.MediaProjectionManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;

public class MessageWebSocketService extends Service {

    private static final String TAG = "MessageWebSocketService";

    public static MessageWebSocketClient mMessageWebSocketClient;
    private final MessageWebSocketBinder mMessageWebSocketBinder = new MessageWebSocketBinder();

    public class MessageWebSocketBinder extends Binder {
        public MessageWebSocketService getService() {
            return MessageWebSocketService.this;
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate");
    }

    @Override
    public IBinder onBind(Intent intent) {
        //获取主机id
        String mid = intent.getStringExtra("mid");
        //初始化数据传输websocketclient
        initMessageWebSocketClient(mid);
        //注册数据传输广播接收器
        registerMessageBroadcastReceiver();
        Log.d(TAG, "onBind");
        return mMessageWebSocketBinder;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mMessageWebSocketClient != null) {
            mMessageWebSocketClient.close();
        }
        Log.d(TAG, "onDestroy");
    }

    //初始化数据传输websocketclient
    public void initMessageWebSocketClient(String mid) {
        URI uri = URI.create(URLUtils.WSURL + mid);
        //连接websocket
        mMessageWebSocketClient = new MessageWebSocketClient(uri) {
            //将接收到的数据传输给广播
            @Override
            public void onMessage(String message) {
                Log.d(TAG, "onMessage");
                Intent intent = new Intent();
                intent.setAction("com.example.remoteassistance.websocket.message");
                intent.putExtra("message", message);
                sendBroadcast(intent);
            }
        };
        mMessageWebSocketClient.connect();
    }

    //数据传输广播接收器
    private class MessageBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String message = intent.getStringExtra("message");
            try {
                ObjectMapper mapper = new ObjectMapper();
                //读取json数据
                MessagePacket packet = mapper.readValue(message, MessagePacket.class);
                //判断命令
                switch (packet.getMsg()) {
                    //触摸事件
                    case "touch":
                        Log.d(TAG, "logTouchEvent");
                        logTouchEvent(packet.getMap());
                        break;
                    default:
                        break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    //通过起始坐标判断触摸操作
    private void logTouchEvent(Map<String, Integer> touch) {
        int staX = touch.get("staX");
        int staY = touch.get("staY");
        int endX = touch.get("endX");
        int endY = touch.get("endY");

        if ((staX - endX) == 0 && (staY - endY) == 0) {
            //点击
            Log.d(TAG, "@" + "input tap" + " " + staX + " " + staY);
        } else if (Math.abs(staX - endX) > 100 || Math.abs(staY - endY) > 100) {
            //滑动
            Log.d(TAG, "@" + "input swipe" + " " + staX + " " + staY + " " + endX + " " + endY);
        }
    }

    //注册数据传输广播接收器
    private void registerMessageBroadcastReceiver() {
        MessageBroadcastReceiver messageBroadcastReceiver = new MessageBroadcastReceiver();
        IntentFilter filter = new IntentFilter("com.example.remoteassistance.websocket.message");
        registerReceiver(messageBroadcastReceiver, filter);
    }
}